
package com.joysuch.wwyt.dataperm.api;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.IOUtils;

public class OneUrlPerLineDataPermissionFilter implements DataPermissionFilter {

    Set<String> enabledUrls = new HashSet<>();
    static final String CLASSPATH_PREFIX = "classpath:";

    public OneUrlPerLineDataPermissionFilter(String resource) throws IOException {
        if (resource.startsWith(CLASSPATH_PREFIX)) {
            enabledUrls.addAll(IOUtils.readLines(OneUrlPerLineDataPermissionFilter.class.getClassLoader()
                    .getResourceAsStream(resource.substring(CLASSPATH_PREFIX.length())), "utf-8"));
        } else {
            enabledUrls.addAll(IOUtils.readLines(new FileInputStream(resource), "utf-8"));
        }
    }

    @Override
    public boolean shouldFilter(String url) {
        return enabledUrls.contains(url);
    }

}
